package com.example.dao;


import javax.naming.NamingException;

import org.apache.directory.shared.ldap.entry.Entry;

import com.example.domain.InetOrgPerson;


public class InetOrgPersonDao extends GenericLdapDao<InetOrgPerson>
{
    public InetOrgPersonDao()
    {
        super.server = "localhost";
        super.port = 12345;
        super.name = "uid=admin,ou=system";
        super.credentials = "secret";
        super.rdnAttribute = "uid";
        super.parentDn = "ou=users,dc=example,dc=com";
        super.attributes = new String[]
            { "objectClass", "uid", "cn", "sn", "givenName", "mail", "telephoneNumber", "userPassword" };
        super.objectClasses = new String[]
            { "inetOrgPerson", "organizationalPerson", "person", "top" };
    }


    protected InetOrgPerson mapFromEntry( Entry entry )
    {
        InetOrgPerson person = new InetOrgPerson();
        person.setUid( getString( entry, "uid" ) );
        person.setFullName( getString( entry, "cn" ) );
        person.setLastName( getString( entry, "sn" ) );
        person.setFirstName( getString( entry, "givenName" ) );
        person.setPassword( getBytes( entry, "userPassword" ) );
        person.setTelephoneNumbers( getStringSet( entry, "telephoneNumber" ) );
        return person;
    }


    protected void mapToEntry( InetOrgPerson person, Entry entry ) throws NamingException
    {
        setString( entry, "uid", person.getUid() );
        setString( entry, "cn", person.getFullName() );
        setString( entry, "sn", person.getLastName() );
        setString( entry, "givenName", person.getFirstName() );
        setBytes( entry, "userPassword", person.getPassword() );
        for ( String telephoneNumber : person.getTelephoneNumbers() )
        {
            setString( entry, "telephoneNumber", telephoneNumber );
        }
    }

}
